function entry (data) {
  this.next = null
  this.data = data
}

function list () {
  let head = null
  let length = 0
  this.add = function (data) {
    let newEntry = new entry(data)
    let current
    if (head === null) {
      head = newEntry
    } else {
      current = head // 将head 存储起来
      while (current.next) {
        current = current.next
      }
      current.next = newEntry
    }
    length++
  }

  this.inset = function (position, data) {
    if (position >= 0 && position <= length) {
      let newEntry = new entry(data)
      let current = head,
        previous,
        index = 0
      if (position === 0) {
        newEntry.next = current
        head = newEntry
      } else {
        while (index++ < position) {
          previous = current
          current = current.next
        }
        newEntry.next = current
        previous.next = newEntry
      }
      length++
      return true
    } else {
      return false
    }
  }

  this.show = function () {
    let current = head;
    for (; current != null; current = current.next) {
      console.log(current.data + '长度' + length)
    }
  }

  this.showIndex = function (data) {
    let current = head
    index = 0
    while (current) {
      if (data === current.data) {
        return index
      }
      index++
      current = current.next
    }
    return -1
  }
}

let listArray = new list()
listArray.add(1)
listArray.add(2)
listArray.add(3)
listArray.inset(4, '随机插入')


listArray.show()
console.log(listArray.showIndex(100))
